package cm.wc.util;

import lombok.extern.slf4j.Slf4j;

import java.io.FileReader;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;

/**
 * @ClassName:demoalligei
 * @Author: 鹿建文
 * @Date: 2022/3/31  - 15:33
 * @Describe:
 */
@Slf4j
public class JDBCUtil {
    private static String url;
    private static String user;
    private static String password;

    static {
        Properties properties = new Properties();
        String propertiesFile = "src/main/resources/mysql.properties";
        try {
            properties.load(new FileReader(propertiesFile));
            log.info("加载数据库连接属性文件{}", propertiesFile);
            //1、加载驱动（可以省略）
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (IOException | ClassNotFoundException e) {
            log.error("属性文件{}不存在", propertiesFile);
            e.printStackTrace();
        }
        url = properties.getProperty("url");
        user = properties.getProperty("user");
        password = properties.getProperty("password");
    }

    private static ThreadLocal<Connection> threadLocal = new ThreadLocal<>();

    public static Connection getConnection() {
        if (threadLocal.get() != null) {
            return threadLocal.get();
        }

        Connection conn = null;
        try {
            conn = DriverManager.getConnection(url, user, password);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        threadLocal.set(conn);
        return conn;
    }

//    public static void main(String[] args) {
//        new Thread(() -> {
//            Connection conn = getConnection();
//            System.out.println(conn);
//            release(conn, null, null);
//        }).start();
//        new Thread(() -> {
//            Connection conn = getConnection();
//            System.out.println(conn);
//            release(conn, null, null);
//        }).start();
//    }

    public static void release(Connection conn, Statement stmt, ResultSet rs) {
        try {
            if (rs != null) {
                rs.close();
                log.info("释放resultset对象{}成功", rs);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            log.error("释放resultset对象{}失败", rs);
        } finally {
            try {
                if (stmt != null) {
                    stmt.close();
                    log.info("释放statement对象{}成功", stmt);
                }
            } catch (SQLException e) {
                e.printStackTrace();
                log.error("释放statement对象{}失败", stmt);
            } finally {
                try {
                    if (conn != null) {
                        conn.close();
                        log.info("释放connection对象{}成功", conn);
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                    log.error("释放connection对象{}失败", conn);
                } finally {
                }
            }
        }
    }

}


